package com.zykj.bootstarter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhoulj(周利军) [1217102780@qq.com]
 * @Title: DemoAutoConfiguration
 * @Project: ${MODEL_NAME}
 * @Module ID:   <(模块)类编号，可以引用系统设计中的类编号>
 * @Comments: <对此类的描述，可以引用系统设计中的描述>
 * @JDK version used:      <JDK1.8> 54
 * @since 2018/12/20-11:54
 */
//标识该类为配置
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
//TODO 存在该类时  初始化该配置
@ConditionalOnClass(DemoService.class)
@ConditionalOnProperty(
        prefix = "demo--prefix",
        value = "demo--value",
        matchIfMissing = true  // 缺失检查
)
public class DemoAutoConfiguration {
    Logger log = LoggerFactory.getLogger(DemoAutoConfiguration.class);
    @Autowired
    private DemoProperties demoProperties;

    @Bean
    //TODO 这里就可以在不用 声明Demoservice Bean 由spring boot 自动判断 Bean 工程没有该类型的bean 就自动创建bean
    @ConditionalOnMissingBean(DemoService.class)
    public DemoService getDemoService() {
        log.debug("调用方法：{getDemoService}");
        DemoService service = new DemoService();
        service.setMsg(demoProperties.getName()+",地址》》》"+demoProperties.getAddress());
        service.setEnableShow(demoProperties.isEnable());
        return service;
    }
}
